package com.hbwxz.common.pojo;

import com.hbwxz.common.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommodityOnSaleBody {
    private List<String> columns;
    private List<Object> values;
    private List<CommodityAttributes> commonAttrs;

    public List<CommodityEsBody> convertToEsBody() {
        List<CommodityEsBody> esBodies = new ArrayList<>();
        values.forEach(val->{
            List<Object> valueList = JsonUtils.readValueFromObject(val, List.class);
            Map<String, Object> map = IntStream.range(0, columns.size()).boxed()
                    .collect(Collectors.toMap(columns::get, valueList::get));
            CommodityEsBody body = JsonUtils.readValueFromObject(map, CommodityEsBody.class);
            body.getAttrs().addAll(commonAttrs);
            esBodies.add(body);
            });

        return esBodies;
    }
}
